package com.cubicleman.airvj.delegates
{
	import com.cubicleman.airvj.Config;
	import com.cubicleman.airvj.model.vo.LyricsRequest;
	import com.universalmind.cairngorm.business.Delegate;
	import com.universalmind.cairngorm.business.ServiceLocator;
	import com.universalmind.cairngorm.events.Callbacks;
	
	import flash.net.URLVariables;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;
	import mx.rpc.soap.mxml.WebService;

	/**
	 *  The MTVDelegate is responsible for interactions with the middleware service.
	 */	
	public class MTVDelegate extends Delegate
	{
		
		//--------------------------------------------------------------------------
	    //
	    //  Variables
	    //
	    //--------------------------------------------------------------------------
	
		/**
		 *  The MarketReplay services.
		 */	
		private var mtvVideoService:HTTPService	= null;
		private var mtvArtistService:HTTPService	= null;
		private var lyricsService:HTTPService = null;
		private var amazonService:WebService = null;
	
		public function MTVDelegate( commandHandlers:IResponder=null )
		{
			super( commandHandlers );
			
			var services:Services	= ServiceLocator.getInstance() as Services;
			mtvArtistService = services.MTVArtistSearchService;
			mtvVideoService = services.MTVVideoSearchService;
			lyricsService = services.lyricsService;
			amazonService = services.amazonWebService;
		}
		
		
		/**
		 * 
		 * search MTV service for video data
		 * 
		 */
		 public function searchVideos( searchString:String ):void
		 {
			 //  Create the URLVariables for the call
			var urlVars:URLVariables 	= new URLVariables();
				urlVars.term			= searchString;
	
			//  Call the service and gets a reference to the AsyncToken for the call 
			var token:AsyncToken = mtvVideoService.send( urlVars );
			
			//	Setup the responder for the result and fault of the call
			var delegateCallbacks:Callbacks 	= new Callbacks( searchVideos_Result,
																	 responder.fault ); 
			prepareHandlers( token, delegateCallbacks );
		} 	
		
		/**
		 *  @private
		 *  Parses the results of the searchVideos call.
		 */
		private function searchVideos_Result( event:ResultEvent ) : void
		{	
			var mtvData:MTVDataTranslator = new MTVDataTranslator();
			var entries:ArrayCollection = new ArrayCollection(mtvData.parseSearchVideoResponse( event.result ));
			responder.result( new ResultEvent( ResultEvent.RESULT, false, true, entries ));
		}	
		
		/**
		 * 
		 * search MTV service for artist data
		 * 
		 */
		 public function searchArtists( searchString:String ):void
		 {
			 //  Create the URLVariables for the call
			var urlVars:URLVariables 	= new URLVariables();
				urlVars.term			= searchString;
	
			//  Call the service and gets a reference to the AsyncToken for the call 
			var token:AsyncToken = mtvArtistService.send( urlVars );
			
			//	Setup the responder for the result and fault of the call
			var delegateCallbacks:Callbacks 	= new Callbacks( searchArtists_Result,
																	 responder.fault ); 
			prepareHandlers( token, delegateCallbacks );
		} 	
		
		/**
		 *  @private
		 *  Parses the results of the searchVideos call.
		 */
		private function searchArtists_Result( event:ResultEvent ) : void
		{	
			var mtvData:MTVDataTranslator = new MTVDataTranslator();
			var entries:ArrayCollection = new ArrayCollection(mtvData.parseSearchArtistResponse( event.result ));
			responder.result( new ResultEvent( ResultEvent.RESULT, false, true, entries ));
		}	
		
		public function lyricsSearch( lyricsRequest:LyricsRequest ):void
		{
			//  Create the URLVariables for the call
			var urlVars:URLVariables 	= new URLVariables();
				urlVars.a = lyricsRequest.artist;
				urlVars.t = lyricsRequest.song;
				urlVars.i = Config.lyricsFlyAPIKey;
	
			//  Call the service and gets a reference to the AsyncToken for the call 
			var token:AsyncToken = lyricsService.send( urlVars );
			
			//	Setup the responder for the result and fault of the call
			var delegateCallbacks:Callbacks 	= new Callbacks( lyricsSearch_Result,
																	 responder.fault ); 
			prepareHandlers( token, delegateCallbacks );
			
		}
		
		/**
		 *  @private
		 *  Parses the results of the searchVideos call.
		 */
		private function lyricsSearch_Result( event:ResultEvent ) : void
		{	
			var mtvData:MTVDataTranslator = new MTVDataTranslator();
			var lyricsRequest:LyricsRequest = mtvData.parseLyricsSearchResponse( event.result );
			responder.result( new ResultEvent( ResultEvent.RESULT, false, true, lyricsRequest ));
		}
		
		
		public function amazonSearch( keyWord:String ):void
		{
			//  Create the URLVariables for the call
			var obj:Object = new Object();
			//obj.Service = 'AWSECommerceService';
			obj.AWSAccessKeyId = Config.AWSAccessKeyId;
			//obj.Operation = 'ItemSearch';
			obj.Shared = new Object();
			obj.Shared.SearchIndex = 'Music';
			obj.Shared.Keywords = keyWord;
			//obj.Version = '2008-08-19';
			obj.Shared.ResponseGroup = "Small,ItemAttributes,Images";
			
			amazonService.ItemSearch.arguments = obj;
			//amazonService.ItemSearch.resultFormat = 'e4x';
			var token:AsyncToken = amazonService.ItemSearch.send();
			//	Setup the responder for the result and fault of the call
			var delegateCallbacks:Callbacks 	= new Callbacks( amazonSearch_Result,
																	 responder.fault ); 
			prepareHandlers( token, delegateCallbacks );
		}
		
		private function amazonSearch_Result( event:ResultEvent ):void
		{trace( event.result );
			var mtvData:MTVDataTranslator = new MTVDataTranslator();
			var resultItems:Array = mtvData.parseAmazonSearchResponse( event.result );
			responder.result( new ResultEvent( ResultEvent.RESULT, false, true, resultItems ));
		}
	}
}